package com.example.goodtrash.Fragment;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodtrash.R;
import com.example.goodtrash.model.News;
import com.example.goodtrash.search.textSearch;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
    private List<News> mNewsList;
    private Context mcontext;
    static class ViewHolder extends RecyclerView.ViewHolder{
        View newsView;
        TextView tvName;
        TextView tvType;
        public ViewHolder(View view){
            super(view);
            newsView=view;
            tvName=(TextView) view.findViewById(R.id.tv_news_name);
            tvType=(TextView) view.findViewById(R.id.tv_news_type);
        }
    }

    public NewsAdapter(List<News> newsList, Context context){
        mNewsList=newsList;
        mcontext=context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot_search,parent,false);
        NewsAdapter.ViewHolder holder=new NewsAdapter.ViewHolder(view);
        holder.newsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=holder.getAdapterPosition();
                News news=mNewsList.get(position);
                Intent intent=new Intent(mcontext, textSearch.class);
                intent.putExtra("hotSearch",news.getName());
                mcontext.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        News news=mNewsList.get(position);
        holder.tvName.setText(news.getName());
        switch (news.getType()){
            case 0:holder.tvType.setText("可回收垃圾");
            case 1:holder.tvType.setText("有害垃圾");
            case 2:holder.tvType.setText("厨余垃圾");
            case 3:holder.tvType.setText("其他垃圾");
        }
    }

    @Override
    public int getItemCount() {
        return mNewsList.size();
    }
}
